import sys
import requests
import json

# 用户名，密码
username = sys.argv[1]
password = sys.argv[2]

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}

# 0.登录
login_params = {'appid': '80', 'isCompressed': '0', 'deviceName': 'iPhone',
                'creditkey': 'ck0.-hYjMz5l0mLDIs3UY_uptD2GsWbOMMvhdu3pKhbHypG7wq-qLFir4NJ0GXUrEJSo8TEi-sN1FpXhfbEMD31fFig2_4X1kPSFaAICoqvl52LSI0zW7XNeVfYTkFCZYoYb',
                'devicesign': 'div101.bbc973a3eb91786413fe935a4390be052173d0c0d93998e1b37ad81ed7729986',
                'providerName': 'CMCC', 'sequenceNo': '5', 'protocolVersion': '301', 'clientVersion': '1.4.0',
                'appName': 'IOS-com.xunlei.mvideo', 'sdkVersion': '4.32.1.4',
                'peerID': 'ffc184a0da5f776f6b24dd59febad8b4', 'deviceModel': 'iPhone14,3', 'passWord': password,
                'OSVersion': 'iOS15.2', 'userName': username, 'netWorkType': 'WIFI', 'platformVersion': '11',
                'customAppName': ''}
login_url = 'https://xluser-ssl.xunlei.com/xluser.core.login/v3/login'
login_params = json.dumps(login_params)
login_req = requests.post(login_url, headers=headers, data=login_params)
login_result = login_req.json()
session_id = login_result['sessionID']
print(session_id)

# sessionId换token
token_params = {'client_id': 'XoL5lqbDWNW0e7QA', 'provider': 'access_end_point_token', 'signin_token': session_id,
                'client_secret': 'XoL5li5BwZkH0vlJ0aCfwk8hiTaV'}
token_url = 'https://xluser-ssl.xunlei.com/v1/auth/signin/token'
token_params = json.dumps(token_params)
token_req = requests.post(token_url, headers=headers, data=token_params)
token_result = token_req.json()
access_token = token_result['access_token']
print(access_token)

# 权限
authorization = 'Bearer ' + access_token
headers['authorization'] = authorization

# 1.获取设备
device_url = 'https://api-pan.xunlei.com/drive/v1/tasks?type=user%23runner'
device_req = requests.get(device_url, headers=headers)
device_result = device_req.json()
device_target = device_result['tasks'][0]['params']['target']

# 2.获取文件信息
file_info_url = 'https://api-pan.xunlei.com/drive/v1/resource/list'
file_url = 'magnet:?xt=urn:btih:906ef14022fb70b9a3d7b412675853b90204fbd6'
file_info_params = {'urls': file_url}
x_captcha_token = 'ck0.rPYiHkv3NvMSVd0quAbOEQ0zamR-pj6STs6bMh' \
                  '-lncPqSN26fdbmXnX629m7wO7jro_RwaqOiPYtzMTXgxCUKoX5SaTl0EMOnD_BulpiLMmCHabakPZcLJpgkRFeFSAUUemr' \
                  'j1cKz3rJuGusaOL42P6CQ16aXbEsioeUPtu604OW3L79r-YoYL9vN55q_ab_sfSpKoQd65hc-qS846C2pkT5V1WcT-vdEO0_f' \
                  'frSNa6oVWjYYn57wU6ItqhcKztfEE5z9zrYvgd8xsM0B-c_ruGc9DW1W622doE9gbo_qhRfoBHFtcXYN_NXGVd4MZ8nC9c7_' \
                  'FQccLcH0ukPUxI-yJstdgPfV1w9WLA3wcwtOpI.ClIIyvCHvN4vEhBYcXAwa0pCWFdod2FUcEI2GgUxLjkuNCIOcGFuLnh1bmxl' \
                  'aS5jb20qIGZmYzE4NGEwZGE1Zjc3NmY2YjI0ZGQ1OWZlYmFkOGI0EoABgUse33Bk7HadC7MfOp6D3yYxVT0R0mTGY8Y8fcvIuSC_' \
                  'jV_Oq-Wat8MS9bqPxW-nvJs1zJQG2l4eucxPZs7b2swkQf6F3OYTWNUNRHKavHQ1SKTgBYU15wZjoqrvxsgYiXHhJNxbdZGqxKOHXO' \
                  '-v0_cq-CQFdhfXH1v4y-mWyRA '
x_device_id = 'ffc184a0da5f776f6b24dd59febad8b4'
file_info_header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                                  'Chrome/60.0.3112.113 Safari/537.36', 'x-captcha-token': x_captcha_token,
                    'x-device-id': x_device_id}
file_info_params = json.dumps(file_info_params)
file_info_req = requests.post(file_info_url, headers=file_info_header, data=file_info_params)
file_info = file_info_req.json()
file_name = file_info['list']['resources'][0]['name']
file_size = file_info['list']['resources'][0]['file_size']
file_count = file_info['list']['resources'][0]['file_count']
print(file_name, file_size, file_count)

# 3.下载
send_task_url = 'https://api-pan.xunlei.com/drive/v1/task'
download_params = {'target': device_target, 'url': file_url, 'total_file_count': str(file_count), 'platform': '',
                   'parent_folder_id': ''}

download_task = {'params': download_params, 'space': device_target, 'type': 'user#download-url', 'file_name': file_name,
                 'file_size': file_size}

download_task = json.dumps(download_task)
task_req = requests.post(send_task_url, headers=headers, data=download_task)
task_info = task_req.json()

print(task_info)
